package org.mat.ipaas.ucenter.service.api.system;

import org.mat.framework.lang.dto.MatApiResponse;

import org.mat.ipaas.ucenter.service.constant.CacheConstant;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysDepartDTO;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysUserDTO;
import org.mat.ipaas.ucenter.service.dto.system.model.DepartIdModelDTO;
import org.mat.ipaas.ucenter.service.dto.system.model.SysDepartTreeModelDTO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 前端控制器
 * <p>
 *
 * @Author: Steve @Since： 2019-01-22
 */
@FeignClient(name = "mat-ipaas-ucenter-service", contextId = "sys-depart-rest-api")
public interface SysDepartRestAPI {

    /**
     * 查询数据 查出我的部门,并以树结构数据格式响应给前端
     *
     * @return
     */
    @RequestMapping(value = "/sys/sysDepart/queryMyDeptTreeList", method = RequestMethod.GET)
    MatApiResponse<List<SysDepartTreeModelDTO>> queryMyDeptTreeList();

    /**
     * 查询数据 查出所有部门,并以树结构数据格式响应给前端
     *
     * @return
     */
    @RequestMapping(value = "/sys/sysDepart/queryTreeList", method = RequestMethod.GET)
    MatApiResponse<List<SysDepartTreeModelDTO>> queryTreeList(@RequestParam(name = "ids", required = false) String ids);

    /**
     * 异步查询部门list
     *
     * @param parentId   父节点 异步加载时传递
     * @param ids        前端回显是传递
     * @param primaryKey 主键字段（id或者orgCode）
     * @return
     */
    @RequestMapping(value = "/sys/sysDepart/queryDepartTreeSync", method = RequestMethod.GET)
    MatApiResponse<List<SysDepartTreeModelDTO>> queryDepartTreeSync(@RequestParam(name = "pid", required = false) String parentId, @RequestParam(name = "ids", required = false) String ids, @RequestParam(name = "primaryKey", required = false) String primaryKey);

    /**
     * 获取某个部门的所有父级部门的ID
     *
     * @param departId 根据departId查
     * @param orgCode  根据orgCode查，departId和orgCode必须有一个不为空
     */
    @GetMapping("/sys/sysDepart/queryAllParentId")
    MatApiResponse queryParentIds(
            @RequestParam(name = "departId", required = false) String departId,
            @RequestParam(name = "orgCode", required = false) String orgCode);

    /**
     * 添加新数据 添加用户新建的部门对象数据,并保存到数据库
     *
     * @param sysDepartDTO
     * @return
     */
    //@RequiresPermissions("system:depart:add")
    @RequestMapping(value = "/sys/sysDepart/add", method = RequestMethod.POST)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    MatApiResponse<SysDepartDTO> add(@RequestHeader("X-Access-Token") String accessToken,
                                     @RequestBody SysDepartDTO sysDepartDTO);

    /**
     * 编辑数据 编辑部门的部分数据,并保存到数据库
     *
     * @param sysDepartDTO
     * @return
     */
    //@RequiresPermissions("system:depart:edit")
    @RequestMapping(value = "/sys/sysDepart/edit", method = {RequestMethod.POST})
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    MatApiResponse<SysDepartDTO> edit(@RequestHeader("X-Access-Token") String accessToken,
                                      @RequestBody SysDepartDTO sysDepartDTO);

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    //@RequiresPermissions("system:depart:delete")
    @RequestMapping(value = "/sys/sysDepart/delete", method = RequestMethod.DELETE)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    MatApiResponse<SysDepartDTO> delete(@RequestParam(name = "id", required = true) String id);


    /**
     * 批量删除 根据前端请求的多个ID,对数据库执行删除相关部门数据的操作
     *
     * @param ids
     * @return
     */
    //@RequiresPermissions("system:depart:deleteBatch")
    @RequestMapping(value = "/sys/sysDepart/deleteBatch", method = RequestMethod.DELETE)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    MatApiResponse<SysDepartDTO> deleteBatch(@RequestParam(name = "ids", required = true) String ids);

    /**
     * 查询数据 添加或编辑页面对该方法发起请求,以树结构形式加载所有部门的名称,方便用户的操作
     *
     * @return
     */
    @RequestMapping(value = "/sys/sysDepart/queryIdTree", method = RequestMethod.GET)
    MatApiResponse<List<DepartIdModelDTO>> queryIdTree();

    /**
     * <p>
     * 部门搜索功能方法,根据关键字模糊搜索相关部门
     * </p>
     *
     * @param keyWord
     * @return
     */
    @RequestMapping(value = "/sys/sysDepart/searchBy", method = RequestMethod.GET)
    MatApiResponse<List<SysDepartTreeModelDTO>> searchBy(@RequestParam(name = "keyWord", required = true) String keyWord, @RequestParam(name = "myDeptSearch", required = false) String myDeptSearch);


    /**
     * 导出excel
     */
    @RequestMapping(value = "/sys/sysDepart/exportXls")
    ModelAndView exportXls(SysDepartDTO sysDepartDTO);

    /**
     * 通过excel导入数据
     * 部门导入方案1: 通过机构编码来计算出部门的父级ID,维护上下级关系;
     * 部门导入方案2: 你也可以改造下程序,机构编码直接导入,先不设置父ID;全部导入后,写一个sql,补下父ID;
     *
     * @return
     */
    //@RequiresPermissions("system:depart:importExcel")
    @RequestMapping(value = "/sys/sysDepart/importExcel", method = RequestMethod.POST)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    MatApiResponse<?> importExcel();

    /**
     * 查询所有部门信息
     *
     * @return
     */
    @GetMapping("listAll")
    MatApiResponse<List<SysDepartDTO>> listAll(@RequestParam(name = "id", required = false) String id);

    /**
     * 查询数据 查出所有部门,并以树结构数据格式响应给前端
     *
     * @return
     */
    @RequestMapping(value = "/sys/sysDepart/queryTreeByKeyWord", method = RequestMethod.GET)
    MatApiResponse<Map<String, Object>> queryTreeByKeyWord(@RequestParam(name = "keyWord", required = false) String keyWord);

    /**
     * 根据部门编码获取部门信息
     *
     * @param orgCode
     * @return
     */
    @GetMapping("/sys/sysDepart/getDepartName")
    MatApiResponse<SysDepartDTO> getDepartName(@RequestParam(name = "orgCode") String orgCode);

    /**
     * 根据部门id获取用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/sys/sysDepart/getUsersByDepartId")
    MatApiResponse<List<SysUserDTO>> getUsersByDepartId(@RequestParam(name = "id") String id);

    /**
     * @param deptIds
     * @return
     * @功能：根据id 批量查询
     */
    @RequestMapping(value = "/sys/sysDepart/queryByIds", method = RequestMethod.GET)
    MatApiResponse<List<SysDepartDTO>> queryByIds(@RequestParam String deptIds);

    @GetMapping("/sys/sysDepart/getMyDepartList")
    MatApiResponse<List<SysDepartDTO>> getMyDepartList();

    /**
     * 异步查询部门list
     *
     * @param parentId 父节点 异步加载时传递
     * @return
     */
    @RequestMapping(value = "/sys/sysDepart/queryBookDepTreeSync", method = RequestMethod.GET)
    MatApiResponse<List<SysDepartTreeModelDTO>> queryBookDepTreeSync(@RequestParam(name = "pid", required = false) String parentId,
                                                                     @RequestParam(name = "tenantId") Integer tenantId,
                                                                     @RequestParam(name = "departName", required = false) String departName);
}
